package com.wica.view;


import java.util.prefs.Preferences;

import com.wica.facetube.R;
import com.wica.media.Player;
import com.wica.model.MovieClip;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

/**
 * Efficient list adapter. The adapter used binds
 * to an ImageView and to a TextView for each row in the list.
 *
 * To work efficiently the adapter implemented here uses two techniques:
 * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
 * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
 *
 * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
 * getView(). This data structures contains references to the views we want to bind data to, thus
 * avoiding calls to findViewById() every time getView() is invoked.
 */
public class EfficientList extends ListActivity {
	
	
	// *************** Constants ************** //	
	// Log constants
	protected static final String LOG_TAG = "EfficientList";
	protected static final String LOG_TAG2 = "FT ==> ";
	
	
	// *************** Attributes ************************* //  
	protected Dialog waitingDialog;

	
	// *************** Methods **************************** //	
    
	/** Called on creation **/
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        waitingDialog = this.onCreateDialog(1);
    }// oncreate method
    
	
    /** Creates waiting dialog **/
    @Override
    protected Dialog onCreateDialog(int id) {
    	if (id==-1){
    		Log.v(LOG_TAG, LOG_TAG+"onCreateDialog(-1) ERROR NO INTERNET");
    		return new AlertDialog.Builder(this)
    	    			.setIcon(R.drawable.stat_sample)
    	        		.setTitle("No Internet connection was found")
    	                .setMessage("You need an internet connection in order to use the application. Try again later.")                
    	                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
    	                    public void onClick(DialogInterface dialog, int whichButton) {
    	                    	finish();  
    	                    }// onclick method
    	                })
    	                .create();
    	}else{
    		// Local variables
    		ProgressDialog dialog;        
    		dialog = new ProgressDialog(this);
    		dialog.setMessage("Please wait while loading...");
    		dialog.setIndeterminate(true);
    		dialog.setCancelable(true);
    		return dialog;
    	}	
    }// onCreateDialog method
    
	
    /** Called when we select a video from the list
     * 	Plays video **/
    protected void onListItemClick(ListView l, View v, int position, long id){
    	// We get movieClip on position "position" from Adapter and we bundle it with intent
    	// Launching the video player
    	Intent i = (new Intent(EfficientList.this, Player.class)).setData(Uri.parse(((MovieClip)getListAdapter().getItem(position)).getFormat1()));
    	String url = ((MovieClip)getListAdapter().getItem(position)).getUrl();
    	i.putExtra("URL", url);    
    	i.putExtra("URLThumbnail", ((MovieClip)getListAdapter().getItem(position)).getURLThumbnail());    	
    	i.putExtra("totalRating", ((MovieClip)getListAdapter().getItem(position)).getTotalRating());
    	i.putExtra("title", ((MovieClip)getListAdapter().getItem(position)).getTitle());
    	i.putExtra("category_tags", ((MovieClip)getListAdapter().getItem(position)).getCategory()+";"+((MovieClip)getListAdapter().getItem(position)).getTags());    	
    	i.putExtra("app_activity_time", getListTimeSetting());
    	String app_activity_string = this.toString();
    	app_activity_string = app_activity_string.split("\\.")[3];
    	app_activity_string = app_activity_string.split("\\@")[0];
    	i.putExtra("app_activity", ""+app_activity_string);
    	Log.v(LOG_TAG, "app_activity:"+app_activity_string);
    	i.putExtra("position", position);
    	i.putExtra("videoLength", ((MovieClip)getListAdapter().getItem(position)).getDuration().toString());
    	this.startActivity(i);    	
    	//    	Log.v(LOG_TAG, LOG_TAG2+Uri.parse(((MovieClip)getListAdapter().getItem(position)).getFormat1()));
    	//    	Log.v(LOG_TAG, LOG_TAG2+((MovieClip)getListAdapter().getItem(position)).getUrl());
    	//    	Log.v(LOG_TAG, LOG_TAG2+((MovieClip)getListAdapter().getItem(position)).getURLThumbnail());
    	//    	Log.v(LOG_TAG, LOG_TAG2+((MovieClip)getListAdapter().getItem(position)).getTotalRating());
    	//    	Log.v(LOG_TAG, LOG_TAG2+((MovieClip)getListAdapter().getItem(position)).getTitle());
    }//onListItemClick method


	protected String getListTimeSetting() {
		return null;		
	}
    
    
}// EfficientList class
